//index.js
//获取应用实例
const app = getApp();

Page({
  data: {
    motto: 'Hello World',
    address: '',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    temp: "4",
    temperature: "-1/10°C",
    type: "晴",
    city: "北京",
    data: "2001-01-01",
    direction: "无持续风向微风级",
    condition: "10",
    futureArray: "[]",
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  onShow: function () {

  },

  onLoad: function () {
    var that = this
    var result = null;
    wx.request({
      url: 'http://apis.juhe.cn/simpleWeather/query',
      data: {
        "city": "成都",
        "key": "b57241e9f22bedf1cf8ce967eb7fdecc"
      },
      success(res) {

        result = res.data.result;
        var test1 = JSON.stringify(result);
        console.log(result)
        that.setData({
          city: res.data.result.city,
          temp: res.data.result.realtime.temperature,
          temperature: res.data.result.future[0].temperature,
          type: res.data.result.future[0].weather,
          direction: res.data.result.future[0].direct,
          date: res.data.result.future[0].date,
          futureArray: res.data.result.future,
        })
        console.log(that.data.futureArray)
      }
    })

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },

  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  getLocation: function () {
    let vm = this;
    wx.getLocation({
      type: 'gcj02',
      altitude: true,
      success: function (res) {
        console.log(JSON.stringify(res))
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy;
        // vm.getLocal(latitude, longitude)
      },
      fail: function (res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  },

  getUserLocation: function () {
    let vm = this;
    wx.getSetting({
      success: (res) => {
        console.log(res)
        res.authSetting['scope.userLocation'] == undefined  //表示 初始化进入该页面
        res.authSetting['scope.userLocation'] == false // 表示 非初始化进入该页面，且未授权
        res.authSetting['scope.userLocation'] == true  //表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      vm.getLocation();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          vm.getLocation();
        }
        else {
          //调用wx.getLocation的API
          vm.getLocation();
        }
      }
    })
  },

  getLocal: function (latitude, longitude) {
    let vm = this; qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      }, success: function (res) {
        console.log(JS0N.stringify(res));
        let province = res.result.ad_info.province
        let address = res.result.formatted_addresses.recommend
        vm.setData({
          province: province,
          address: address,
          latitude: latitude,
          longitude: longitude
        })
      }
    })
  },

  fail: function (res) {
    console.log(res);
  }, complete: function (res) {
    //console. log(res);
  },

})


